# coding=utf-8

"""
label：通过广度优先搜索，把一个细胞核对应的所有位置都标记为2
photo：细胞图片，是一个由list构成的list
line，col：当前细胞核的位置
"""
def label(photo,line,col):
    photo[line][col] = 2
    if line>0 and photo[line-1][col] == 1:
        label(photo,line-1,col)
    if line<lines-1 and photo[line+1][col] == 1:
        label(photo,line+1,col)
    if col>0 and photo[line][col-1] == 1:
        label(photo,line,col-1)
    if col<cols-1 and photo[line][col+1] == 1:
        label(photo,line,col+1)

# 保存图片
photo = []   

#读取行数和列数
lines,cols = map(int,input().split())  
print(lines,cols)

# 读取图片->photo
for i in range(lines):
    line = input()
    line = list(map(int,line))
    print(line)
    photo.append(line)

#细胞计数
num = 0
for i in range(lines):
    for j in range(cols):
        if photo[i][j] == 1:
            num = num + 1
            label(photo,i,j)
            
print(num)